package mil.cdc.utils;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.attribute.HashPrintRequestAttributeSet;

public class Printer {

	
	public Printer() { }
	
	public boolean print(final String printString) {	
		
		
		final String[] str = printString.split("\n");
		
		PrinterJob pj = PrinterJob.getPrinterJob();
		PageFormat pf = pj.defaultPage();
		pj.setPrintable(new Printable() {
			
			@Override
			public int print(Graphics graphics, PageFormat pf, int pageIndex)
					throws PrinterException {
				if (pageIndex > 0) {
					return Printable.NO_SUCH_PAGE;
				}
		        Graphics2D g2d = (Graphics2D) graphics;
		        g2d.translate(pf.getImageableX(), pf.getImageableY());
		        
		        for(int i = 0; i < str.length; i++) {
		        	String s = str[i];
		        	s.replace("\t", "     ");
		        	g2d.drawString(s, 50, 50 + i * 20);	
		        }
		        return Printable.PAGE_EXISTS;
			}
		}, pf);
		
		if (pj.printDialog(new HashPrintRequestAttributeSet())) {
			try {
				pj.print();
				return true;
			} catch (PrinterException e) { }
		}
		return false;
	}
}


